# Maintainer: David Runge <dvzrv@archlinux.org>

_name=pmix
pkgbase=openpmix
pkgname=(openpmix openpmix-docs)
pkgver=4.2.3
pkgrel=2
pkgdesc="Extended version of the PMI standard"
arch=(x86_64)
url="https://github.com/openpmix/openpmix"
license=(BSD)
makedepends=(glibc hwloc perl libevent libevent python zlib)
source=($url/releases/download/v$pkgver/$_name-$pkgver.tar.gz)
sha512sums=('824c318848abf7a7c6dbb2a4cb06ec75d573b40c360798b6d35671135fbb63d42dba7c16c65a3261fb31ad902ff10d8256a387ead1e6afd65f65329b0b6a4dda')
b2sums=('b916c2dc3143d95309c8a1698a7477563d5faec1014a3cd4ae56cbbad74be3fc6310b28be87bd2e15bc759a6b9b281cf9e1e19449dbdbfe16d0c1b088d5d724d')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

prepare() {
  cd $_name-$pkgver
  ./autogen.pl
}

build() {
  local configure_options=(
    --prefix=/usr
    --sysconfdir=/etc/$pkgname
  )

  cd $_name-$pkgver
  ./configure "${configure_options[@]}"
  # prevent excessive overlinking due to libtool
  sed -i -e 's/ -shared / -Wl,-O1,--as-needed\0/g' libtool
  make V=1
}

check() {
  make -k check -C $_name-$pkgver
}

package_openpmix() {
  depends=(
    glibc
    hwloc
    libevent libevent_{core,pthreads}-2.1.so
    zlib
  )
  optdepends=(
    'openpmix-docs: for documentation'
  )
  provides=(libpmix.so pmix)
  backup=(etc/$pkgname/pmix-mca-params.conf)

  make DESTDIR="$pkgdir" install -C $_name-$pkgver

  (
    cd "$pkgdir"
    _pick $pkgbase-docs usr/share/doc/
  )

  install -vDm 644 $_name-$pkgver/LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}

package_openpmix-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir/"
}
